#include "../Include/GcpProxy.h"
#include "../Include/GcpClientEvent.h"
#include <Http/HttpReqMessage.h>
#include <Http/HttpResMessage.h>

CGcpProxy::CGcpProxy(CGcpClientEvent *pClient)
{
    CSocketAddr addr = SocketUtil::GetRemoteAddress(pClient->GetFD());
    m_strPeerAddress = addr.GetAddrString();

    m_pClient = pClient;
    pClient->AddRef();
}

CGcpProxy::~CGcpProxy()
{
    Close();
    m_pClient->Release();
    m_pClient = NULL;
}

Bool CGcpProxy::Request(LPCSTR lpszUri, const string &content, LPCSTR lpszContentType)
{
    CHttpReqMessage msg;
    msg.SetRequestLine("POST", lpszUri, "HTTP/1.1");
    msg.SetBody(content, lpszContentType);

    m_pClient->Send(msg.GetPacket(), True);
    return True;
}

Bool CGcpProxy::Respond(const string &content, LPCSTR lpszContentType)
{
    CHttpResMessage msg;
    msg.SetCode(200, "OK", "HTTP/1.1");
    msg.SetBody(content, lpszContentType);

    m_pClient->Send(msg.GetPacket(), True);
    return True;
}

Bool CGcpProxy::IsConnected() const
{
    return m_pClient->IsConnected();
}

void CGcpProxy::Close()
{
    m_pClient->CloseConnection();
}
